home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / qttojavaimage / src / qttojavaimage.java
Encoding:
Java Source  |  2000-09-28  |  3.8 KB  |  115 lines

  1. /*
  2.  * quicktime.app: Sample Code for Initial Seeding
  3.  *
  4.  * © 1996, 97 Copyright, Apple Computer
  5.  * All rights reserved
  6.  */
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10.  
  11. import quicktime.*;
  12. import quicktime.io.*;
  13. import quicktime.qd.*;
  14. import quicktime.std.image.*;
  15. import quicktime.app.image.*;
  16.  
  17. /*
  18.     This sample code shows how to create a java.awt.Image out 
  19.     of some image that QuickTime produces.
  20.     
  21.     The source of the QuickTime image could come from any one of:
  22.         (1) An image file in a format that Java doesn't directly support but QT does
  23.         (2) Recording the drawing actions of a QDGraphics into a Pict -> this can be
  24.         written out to a file or presented by an ImagePresenter class to the QTIMageProducer directly
  25.         (3) Using the services of QuickTime's SequenceGrabbing component. A SequenceGrabber
  26.         can be used to capture just an individual frame from a video source (the SGCapture shows basic
  27.         usage of the SequenceGrabber and the QT documentation has more details on these components)
  28.  
  29.     In this code the user is prompted to open an image file (one of 20+ formats that QuickTime's
  30.     GraphicsImporter can import)
  31.     
  32.     The program then uses the QTImageProducer to create a java.awt.Image which is then drawn
  33.     in the paint method of the Frame
  34. */
  35. public class QTtoJavaImage extends Frame {    
  36.     
  37.     public static void main (String args[]) {
  38.         try {
  39.             QTSession.open ();            
  40.             QTtoJavaImage window = new QTtoJavaImage("QT in Java");
  41.                 // this will lay out and resize the Frame to the size of the selected movie
  42.             window.pack();
  43.             window.show();
  44.             window.toFront();
  45.         } catch (QTException e) {
  46.                 // catch a userCanceledErr and just exit the program
  47.             if (e.errorCode() == Errors.userCanceledErr) {
  48.                 QTSession.close();
  49.                 System.exit(0);
  50.             }
  51.                 // some other error occured - print out a stack trace
  52.                 // and close the QTSession
  53.             e.printStackTrace();
  54.             QTSession.close();
  55.         }
  56.     }
  57.  
  58.     QTtoJavaImage (String title) throws QTException {
  59.         super (title);
  60.             
  61.             // prompt the user to select an image file
  62.         QTFile imageFile = QTFile.standardGetFilePreview(QTFile.kStandardQTFileTypes);
  63.         
  64.             // import the image into QuickTime
  65.         GraphicsImporter myGraphicsImporter = new GraphicsImporter (imageFile);
  66.         
  67.             //Create a GraphicsImporterDrawer which uses the GraphicsImporter to draw
  68.             //this object produces pixels for the QTImageProducer
  69.         GraphicsImporterDrawer myDrawer = new GraphicsImporterDrawer (myGraphicsImporter);
  70.             
  71.             //Create a java.awt.Image from the pixels supplied to it by the QTImageProducer
  72.         QDRect r = myDrawer.getDisplayBounds();
  73.             // this is the size of the image - this will become the size of the frame
  74.         imageSize = new Dimension (r.getWidth(), r.getHeight());
  75.         QTImageProducer qtProducer = new QTImageProducer (myDrawer, imageSize);
  76.         javaImage = Toolkit.getDefaultToolkit().createImage(qtProducer);
  77.  
  78.             // add a Window Listener to this frame 
  79.             // that will close down the QTSession, dispose of the Frame
  80.             // which will close the window - where we exit
  81.         addWindowListener(new WindowAdapter () {
  82.             public void windowClosing (WindowEvent e) {
  83.                 QTSession.close();
  84.                 dispose();
  85.             }
  86.  
  87.             public void windowClosed (WindowEvent e) { 
  88.                 System.exit(0);
  89.             }
  90.         });
  91.     }
  92.     
  93.     Image javaImage = null;
  94.     Dimension imageSize;
  95.     
  96.     public void paint (Graphics g) {
  97.         Insets i = getInsets();
  98.         Dimension d = getSize();
  99.         int width = d.width - i.left - i.right;
  100.         int height = d.height - i.top - i.bottom;
  101.             //make sure image is scaled correctly to fill the entire visible area of the frame
  102.         g.drawImage (javaImage, i.left, i.top, width, height, this);
  103.     }
  104.     
  105.         //this returns the size of the image - so the pack will correctly resize the frame
  106.     public Dimension getPreferredSize () {
  107.         return imageSize;
  108.     }
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.